/*
 *  The MIT License
 *
 *  Copyright 2010 marco.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
package com.elogiclab.springsecurity.gae.auth;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.security.authentication.AuthenticationDetailsSourceImpl;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.MutableGrantedAuthoritiesContainer;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedGrantedAuthoritiesAuthenticationDetails;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 *
 * @author marco
 */
public class GoogleAccountsAuthenticationDetailsSource extends AuthenticationDetailsSourceImpl {

    private static final Logger logger = Logger.getLogger(GoogleAccountsAuthenticationDetailsSource.class.getName());

    public GoogleAccountsAuthenticationDetailsSource() {
        super.setClazz(PreAuthenticatedGrantedAuthoritiesAuthenticationDetails.class);
    }

    @Override
    public Object buildDetails(Object context) {
        Object result = super.buildDetails(context);
        logger.info("Entering buildDetails");
        if (result instanceof MutableGrantedAuthoritiesContainer) {
            MutableGrantedAuthoritiesContainer mac = (MutableGrantedAuthoritiesContainer) result;
            UserService userService = UserServiceFactory.getUserService();
            if (userService.isUserAdmin()) {
                logger.log(Level.INFO, "User {0} is an admin", userService.getCurrentUser().getUserId());
                List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
                ga.add(new GrantedAuthorityImpl("ROLE_GOOGLE_APP_ADMIN"));
                ga.add(new GrantedAuthorityImpl("ROLE_ADMIN_USER"));
                ga.add(new GrantedAuthorityImpl("ROLE_GOOGLE_APP_USER"));
                ga.add(new GrantedAuthorityImpl("ROLE_AUTHENTICATED_USER"));
                ga.add(new GrantedAuthorityImpl("GOOGLE_AUTH_DOMAIN_"
                        + userService.getCurrentUser().getAuthDomain()));

                mac.setGrantedAuthorities(ga);
            } else if (userService.isUserLoggedIn()) {
                logger.log(Level.INFO, "User {0} is normal user", userService.getCurrentUser().getUserId());
                List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
                ga.add(new GrantedAuthorityImpl("ROLE_GOOGLE_APP_USER"));
                ga.add(new GrantedAuthorityImpl("ROLE_AUTHENTICATED_USER"));
                ga.add(new GrantedAuthorityImpl("GOOGLE_AUTH_DOMAIN_"
                        + userService.getCurrentUser().getAuthDomain()));

                mac.setGrantedAuthorities(ga);
            }
        }
        return result;
    }
}
